home *** CD-ROM | disk | FTP | other *** search
- /********************************************/
- /* */
- /* ResPlotter Code from Column Six of */
- /* */
- /* *** MacTutor, Getting Started *** */
- /* */
- /* Copyright 1992, Dave Mark */
- /* */
- /********************************************/
-
- #define kBaseResID 128
- #define kMoveToFront (WindowPtr)-1
- #define kRandomUpperLimit 32768
-
-
- /***************/
- /* Functions */
- /***************/
-
- void ToolBoxInit( void );
- void WindowInit( void );
- void MainLoop( void );
- void DrawRandomIcon( CIconHandle theIcon );
- void RandomPoint( Point *pointPtr );
- short Randomize( short range );
-
-
- /****************** main ***************************/
-
- void main( void )
- {
- ToolBoxInit();
- WindowInit();
- MainLoop();
- }
-
-
- /****************** ToolBoxInit *********************/
-
- void ToolBoxInit( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- }
-
-
- /****************** WindowInit ***********************/
-
- void WindowInit( void )
- {
- WindowPtr window;
- StringHandle windowTitleH;
-
- window = GetNewWindow( kBaseResID , nil,
- kMoveToFront );
-
- if ( window == nil )
- {
- SysBeep( 10 ); /* Couldn't load the WIND resource!!! */
- ExitToShell();
- }
-
- windowTitleH = GetString( kBaseResID );
-
- if ( windowTitleH == nil )
- {
- SysBeep( 10 ); /* Couldn't load the STR resource!!! */
- ExitToShell();
- }
-
- HLock( (Handle)windowTitleH );
- SetWTitle( window, *windowTitleH );
- HUnlock( (Handle)windowTitleH );
-
- ShowWindow( window );
- SetPort( window );
- }
-
-
- /****************** MainLoop ***********************/
-
- void MainLoop( void )
- {
- CIconHandle theIcon;
-
- GetDateTime( (unsigned long *)(&randSeed) );
-
- theIcon = GetCIcon( kBaseResID );
-
- if ( theIcon == nil )
- {
- SysBeep( 10 ); /* Couldn't load the cicn resource!!! */
- ExitToShell();
- }
-
- while ( ! Button() )
- DrawRandomIcon( theIcon );
- }
-
-
- /****************** DrawRandomIcon *****************/
-
- void DrawRandomIcon( CIconHandle theIcon )
- {
- Point p;
- Rect iconRect;
-
- RandomPoint( &p );
-
- SetRect( &iconRect, p.h, p.v, p.h+32, p.v+32 );
- PlotCIcon( &iconRect, theIcon );
- }
-
-
- /****************** RandomPoint *********************/
-
- void RandomPoint( Point *pointPtr )
- {
- WindowPtr window;
-
- window = FrontWindow();
-
- pointPtr->h = Randomize( window->portRect.right
- - window->portRect.left );
- pointPtr->v = Randomize( window->portRect.bottom
- - window->portRect.top );
- }
-
-
- /****************** Randomize **********************/
-
- short Randomize( short range )
- {
- long randomNumber;
-
- randomNumber = Random();
-
- if ( randomNumber < 0 )
- randomNumber *= -1;
-
- return( (randomNumber * range) / kRandomUpperLimit );
- }